home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 6 / CU Amiga Magazine's Super CD-ROM 06 (1996)(EMAP Images)(GB)(Track 1 of 4)[!][issue 1997-01].iso / cucd / prog / gnu-c / src / gcc-2.7.0-amiga / cp / spew.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  12KB  |  449 lines

  1. /* Type Analyzer for GNU C++.
  2.    Copyright (C) 1987, 1989, 1992, 1993 Free Software Foundation, Inc.
  3.    Hacked... nay, bludgeoned... by Mark Eichin (eichin@cygnus.com)
  4.  
  5. This file is part of GNU CC.
  6.  
  7. GNU CC is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU CC is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU CC; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22.  
  23. /* This file is the type analyzer for GNU C++.  To debug it, define SPEW_DEBUG
  24.    when compiling parse.c and spew.c.  */
  25.  
  26. #include "config.h"
  27. #include <stdio.h>
  28. #include "input.h"
  29. #include "tree.h"
  30. #include "lex.h"
  31. #include "parse.h"
  32. #include "cp-tree.h"
  33. #include "flags.h"
  34. #include "obstack.h"
  35.  
  36. /* This takes a token stream that hasn't decided much about types and
  37.    tries to figure out as much as it can, with excessive lookahead and
  38.    backtracking. */
  39.  
  40. /* fifo of tokens recognized and available to parser. */
  41. struct token  {
  42.   /* The values for YYCHAR will fit in a short.  */
  43.   short        yychar;
  44.   short        end_of_file;
  45.   YYSTYPE    yylval;
  46. };
  47.  
  48. static int do_aggr ();
  49.  
  50. /* From lex.c: */
  51. /* the declaration found for the last IDENTIFIER token read in.
  52.    yylex must look this up to detect typedefs, which get token type TYPENAME,
  53.    so it is left around in case the identifier is not a typedef but is
  54.    used in a context which makes it a reference to a variable.  */
  55. extern tree lastiddecl;        /* let our brains leak out here too */
  56. extern int    yychar;        /*  the lookahead symbol        */
  57. extern YYSTYPE    yylval;        /*  the semantic value of the        */
  58.                 /*  lookahead symbol            */
  59. extern int end_of_file;
  60.  
  61. struct obstack token_obstack;
  62. int first_token;
  63.   
  64. #ifdef SPEW_DEBUG
  65. int spew_debug = 0;
  66. static unsigned int yylex_ctr = 0;
  67. static int debug_yychar ();
  68. #endif
  69.  
  70. /* Initialize token_obstack. Called once, from init_lex.  */
  71. void
  72. init_spew ()
  73. {
  74.   gcc_obstack_init(&token_obstack);
  75. }
  76.  
  77. #ifdef SPEW_DEBUG
  78. /* Use functions for debugging...  */
  79.  
  80. /* Return the number of tokens available on the fifo. */
  81. static int
  82. num_tokens ()
  83. {
  84.   return (obstack_object_size(&token_obstack)/sizeof(struct token))
  85.     - first_token;
  86. }
  87.  
  88. /* Fetch the token N down the line from the head of the fifo. */
  89. static struct token*
  90. nth_token (n)
  91.      int n;
  92. {
  93.   /* could just have this do slurp_ implicitly, but this way is easier
  94.    * to debug... */
  95.   my_friendly_assert (n < num_tokens(), 298);
  96.   return ((struct token*)obstack_base(&token_obstack))+n+first_token;
  97. }
  98.  
  99. /* Add a token to the token fifo. */
  100. static void
  101. add_token (t)
  102.      struct token* t;
  103. {
  104.   obstack_grow(&token_obstack,t,sizeof (struct token));
  105. }
  106.  
  107. /* Consume the next token out of the fifo.  */
  108. static void
  109. consume_token()
  110. {
  111.   if (num_tokens() == 1)
  112.     {
  113.       obstack_free(&token_obstack, obstack_base (&token_obstack));
  114.       first_token = 0;
  115.     }
  116.   else
  117.     first_token++;
  118. }
  119.  
  120. #else
  121. /* ...otherwise use macros.  */
  122.  
  123. #define num_tokens() \
  124.   ((obstack_object_size(&token_obstack)/sizeof(struct token)) - first_token)
  125.  
  126. #define nth_token(N) \
  127.   (((struct token*)obstack_base(&token_obstack))+(N)+first_token)
  128.  
  129. #define add_token(T) obstack_grow(&token_obstack, (T), sizeof (struct token))
  130.  
  131. #define consume_token() \
  132.   (num_tokens() == 1                            \
  133.    ? (obstack_free (&token_obstack, obstack_base (&token_obstack)),    \
  134.       (first_token = 0))                        \
  135.    : first_token++)
  136. #endif
  137.  
  138. /* Pull in enough tokens from real_yylex that the queue is N long beyond
  139.    the current token.  */
  140.  
  141. static void
  142. scan_tokens (n)
  143.      int n;
  144. {
  145.   int i;
  146.   struct token *tmp;
  147.  
  148.   /* We cannot read past certain tokens, so make sure we don't.  */
  149.   i = num_tokens ();
  150.   if (i > n)
  151.     return;
  152.   while (i-- > 0)
  153.     {
  154.       tmp = nth_token (i);
  155.       /* Never read past these characters: they might separate
  156.      the current input stream from one we save away later.  */
  157.       if (tmp->yychar == '{' || tmp->yychar == ':' || tmp->yychar == ';')
  158.     goto pad_tokens;
  159.     }
  160.  
  161.   while (num_tokens() <= n)
  162.     {
  163.       obstack_blank(&token_obstack,sizeof (struct token));
  164.       tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
  165.       tmp->yychar = real_yylex();
  166.       tmp->end_of_file = end_of_file;
  167.       tmp->yylval = yylval;
  168.       end_of_file = 0;
  169.       if (tmp->yychar == '{'
  170.       || tmp->yychar == ':'
  171.       || tmp->yychar == ';')
  172.     {
  173.     pad_tokens:
  174.       while (num_tokens () <= n)
  175.         {
  176.           obstack_blank(&token_obstack,sizeof (struct token));
  177.           tmp = ((struct token *)obstack_next_free (&token_obstack))-1;
  178.           tmp->yychar = EMPTY;
  179.           tmp->end_of_file = 0;
  180.         }
  181.     }
  182.     }
  183. }
  184.  
  185. /* Create room for N tokens at the front of the fifo.  This is used
  186.    to insert new tokens into the stream ahead of the current token.  */
  187.  
  188. static void
  189. shift_tokens (n)
  190.      int n;
  191. {
  192.   if (first_token >= n)
  193.     first_token -= n;
  194.   else
  195.     {
  196.       int old_token_count = num_tokens ();
  197.       char *tmp;
  198.  
  199.       obstack_blank (&token_obstack, (n-first_token) * sizeof (struct token));
  200.       if (old_token_count)
  201.     {
  202.       tmp = (char *)alloca ((num_tokens () + (n-first_token))
  203.                 * sizeof (struct token));
  204.       /* This move does not rely on the system being able to handle
  205.          overlapping moves.  */
  206.       bcopy ((char *) nth_token (0), tmp,
  207.          old_token_count * sizeof (struct token));
  208.       bcopy (tmp, (char *) nth_token (n),
  209.          old_token_count * sizeof (struct token));
  210.     }
  211.       first_token = 0;
  212.     }
  213. }
  214.  
  215. static int
  216. probe_obstack (h, obj, nlevels)
  217.      struct obstack *h;
  218.      tree obj;
  219.      unsigned int nlevels;
  220. {
  221.   register struct _obstack_chunk*  lp;    /* below addr of any objects in this chunk */
  222.   register struct _obstack_chunk*  plp;    /* point to previous chunk if any */
  223.  
  224.   lp = (h)->chunk;
  225.   /* We use >= rather than > since the object cannot be exactly at
  226.      the beginning of the chunk but might be an empty object exactly
  227.      at the end of an adjacent chunk. */
  228.   for (; nlevels != 0 && lp != 0 && ((tree)lp >= obj || (tree)lp->limit < obj);
  229.        nlevels -= 1)
  230.     {
  231.       plp = lp->prev;
  232.       lp = plp;      
  233.     }
  234.   return nlevels != 0 && lp != 0;
  235. }
  236.  
  237. /* from lex.c: */
  238. /* Value is 1 (or 2) if we should try to make the next identifier look like
  239.    a typename (when it may be a local variable or a class variable).
  240.    Value is 0 if we treat this name in a default fashion. */
  241. extern int looking_for_typename;
  242. int looking_for_template;
  243.  
  244. extern struct obstack *current_obstack, *saveable_obstack;
  245. tree got_scope;
  246. tree got_object;
  247.  
  248. int
  249. yylex()
  250. {
  251.   struct token tmp_token;
  252.   tree trrr;
  253.  
  254.  retry:
  255. #ifdef SPEW_DEBUG
  256.   if (spew_debug)
  257.   {
  258.     yylex_ctr ++;
  259.     fprintf(stderr, "\t\t## %d ##",yylex_ctr);
  260.   }
  261. #endif
  262.  
  263.   /* if we've got tokens, send them */
  264.   if (num_tokens())
  265.     {
  266.       tmp_token= *nth_token(0);
  267.  
  268.       /* TMP_TOKEN.YYLVAL.TTYPE may have been allocated on the wrong obstack.
  269.      If we don't find it in CURRENT_OBSTACK's current or immediately
  270.      previous chunk, assume it was and copy it to the current obstack.  */
  271.       if ((tmp_token.yychar == CONSTANT
  272.        || tmp_token.yychar == STRING)
  273.       && ! TREE_PERMANENT (tmp_token.yylval.ttype)
  274.       && ! probe_obstack (current_obstack, tmp_token.yylval.ttype, 2)
  275.       && ! probe_obstack (saveable_obstack, tmp_token.yylval.ttype, 2))
  276.     tmp_token.yylval.ttype = copy_node (tmp_token.yylval.ttype);
  277.     }
  278.   else
  279.     {
  280.       /* if not, grab the next one and think about it */
  281.       tmp_token.yychar = real_yylex ();
  282.       tmp_token.yylval = yylval;
  283.       tmp_token.end_of_file = end_of_file;
  284.       add_token(&tmp_token);
  285.     }
  286.  
  287.   /* many tokens just need to be returned. At first glance, all we
  288.    * have to do is send them back up, but some of them are needed to
  289.    * figure out local context. */
  290.   switch(tmp_token.yychar)
  291.     {
  292.     case EMPTY:
  293.       /* This is a lexical no-op.  */
  294.       consume_token ();
  295. #ifdef SPEW_DEBUG    
  296.       if (spew_debug)
  297.     debug_yychar (tmp_token.yychar);
  298. #endif
  299.       goto retry;
  300.  
  301.     case IDENTIFIER:
  302.       scan_tokens (1);
  303.       if (nth_token (1)->yychar == SCOPE)
  304.     /* Don't interfere with the setting from an 'aggr' prefix.  */
  305.     looking_for_typename++;
  306.       else if (nth_token (1)->yychar == '<')
  307.     looking_for_template = 1;
  308.  
  309.       trrr = lookup_name (tmp_token.yylval.ttype, -2);
  310.  
  311.       if (trrr)
  312.     {
  313.       tmp_token.yychar = identifier_type (trrr);
  314.       switch (tmp_token.yychar)
  315.         {
  316.         case TYPENAME:
  317.           lastiddecl = identifier_typedecl_value (tmp_token.yylval.ttype);
  318.           if (lastiddecl != trrr)
  319.         {
  320.           lastiddecl = trrr;
  321.           if (got_scope || got_object)
  322.             tmp_token.yylval.ttype = DECL_NESTED_TYPENAME (trrr);
  323.         }
  324.           break;
  325.         case IDENTIFIER:
  326.           lastiddecl = trrr;
  327.           break;
  328.         case PTYPENAME:
  329.           lastiddecl = NULL_TREE;
  330.           break;
  331.         case NSNAME:
  332.           lastiddecl = trrr;
  333.           if (got_scope || got_object)
  334.         tmp_token.yylval.ttype = trrr;
  335.           break;
  336.         default:
  337.           my_friendly_abort (101);
  338.         }
  339.     }
  340.       else
  341.     lastiddecl = trrr;
  342.       got_scope = NULL_TREE;
  343.       /* and fall through to... */
  344.     case IDENTIFIER_DEFN:
  345.     case TYPENAME:
  346.     case TYPENAME_DEFN:
  347.     case PTYPENAME:
  348.     case PTYPENAME_DEFN:
  349.       consume_token ();
  350.       if (looking_for_typename > 0)
  351.     looking_for_typename--;
  352.       looking_for_template = 0;
  353.       break;
  354.  
  355.     case SCSPEC:
  356.       /* do_aggr needs to check if the previous token was RID_FRIEND,
  357.      so just increment first_token instead of calling consume_token. */
  358.       first_token++;
  359.       break;
  360.     case TYPESPEC:
  361.       consume_token ();
  362.       break;
  363.  
  364.     case AGGR:
  365.       *nth_token(0) = tmp_token;
  366.       do_aggr ();
  367.       /* fall through to output... */
  368.     case ENUM:
  369.       /* Set this again, in case we are rescanning.  */
  370.       looking_for_typename = 1;
  371.       /* fall through... */
  372.     default:
  373.       consume_token();
  374.     }
  375.  
  376.   yylval = tmp_token.yylval;
  377.   yychar = tmp_token.yychar;
  378.   end_of_file = tmp_token.end_of_file;
  379. #ifdef SPEW_DEBUG    
  380.   if (spew_debug)
  381.     debug_yychar(yychar);
  382. #endif
  383.   return yychar;
  384. }
  385.  
  386. /* token[0] == AGGR (struct/union/enum)
  387.  * Thus, token[1] is either a TYPENAME or a TYPENAME_DEFN.
  388.  * If token[2] == '{' or ':' then it's TYPENAME_DEFN.
  389.  * It's also a definition if it's a forward declaration (as in 'struct Foo;')
  390.  * which we can tell lf token[2] == ';' *and* token[-1] != FRIEND.
  391.  */
  392. static int
  393. do_aggr ()
  394. {
  395.   int yc1, yc2;
  396.   
  397.   scan_tokens (2);
  398.   yc1 = nth_token (1)->yychar;
  399.   if (yc1 != TYPENAME && yc1 != IDENTIFIER && yc1 != PTYPENAME)
  400.     return 0;
  401.   yc2 = nth_token (2)->yychar;
  402.   if (yc2 == ';')
  403.     {
  404.       /* It's a forward declaration iff we were not preceded by 'friend'. */
  405.       if (first_token > 0 && nth_token (-1)->yychar == SCSPEC
  406.       && nth_token (-1)->yylval.ttype == ridpointers[(int) RID_FRIEND])
  407.     return 0;
  408.     }
  409.   else if (yc2 != '{' && yc2 != ':')
  410.     return 0;
  411.  
  412.   switch (yc1)
  413.     {
  414.     case TYPENAME:
  415.       nth_token (1)->yychar = TYPENAME_DEFN;
  416.       break;
  417.     case PTYPENAME:
  418.       nth_token (1)->yychar = PTYPENAME_DEFN;
  419.       break;
  420.     case IDENTIFIER:
  421.       nth_token (1)->yychar = IDENTIFIER_DEFN;
  422.       break;
  423.     default:
  424.       my_friendly_abort (102);
  425.     }
  426.   return 0;
  427. }  
  428.   
  429. #ifdef SPEW_DEBUG    
  430. /* debug_yychar takes a yychar (token number) value and prints its name. */
  431. static int
  432. debug_yychar (yy)
  433.      int yy;
  434. {
  435.   /* In parse.y: */
  436.   extern char *debug_yytranslate ();
  437.   
  438.   int i;
  439.   
  440.   if(yy<256) {
  441.     fprintf (stderr, "<%d: %c >\n", yy, yy);
  442.     return 0;
  443.   }
  444.   fprintf (stderr, "<%d:%s>\n", yy, debug_yytranslate (yy));
  445.   return 1;
  446. }
  447.  
  448. #endif
  449.